﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace TicTacToeX {
	public static class GameLogicLayer {

		public static void UpdatePlayGame(Game currentGame) {
			Input.UpdateInputState();
			Input.UpdateMousePosition();

			if(Table.CurrentPlayer == Table.Player.One) {
				for(var nY = 0;nY < Table.Collumns;nY++) {
					for(var nX = 0;nX < Table.Rows;nX++) {

						Vector2 gridPos = new Vector2(nX,nY);
						Vector2 finalPos = Table.Corner + Size.Grid * gridPos;

						if((Input.MouseState.LeftButton == ButtonState.Pressed && Input.MouseStateAux.LeftButton != ButtonState.Pressed)) {
							Table.ApplyMove(finalPos,gridPos,Table.CurrentPlayer,Table.Player.Two,'X');
						}
					}
				}
			} else if(Table.CurrentPlayer == Table.Player.Two) {
				for(var nY = 0;nY < Table.Collumns;nY++) {
					for(var nX = 0;nX < Table.Rows;nX++) {

						Vector2 gridPos = new Vector2(nX,nY);
						Vector2 finalPos = Table.Corner + Size.Grid * gridPos;

						if((Input.MouseState.LeftButton == ButtonState.Pressed && Input.MouseStateAux.LeftButton != ButtonState.Pressed)) {
							Table.ApplyMove(finalPos,gridPos,Table.CurrentPlayer,Table.Player.One,'O');
						}
					}
				}
			}
			Input.RunEscape(currentGame);
			Input.MouseStateAux = Input.MouseState;
			Input.MouseState = Mouse.GetState();
		}

		public static void DrawPlayGame() {
			Assets.SpriteBatch.Begin();

			for(var nY = 0;nY < Table.Collumns;nY++) {
				for(var nX = 0;nX < Table.Rows;nX++) {
					var vGridPos = new Vector2(nX,nY);
					var vFinalPos = Table.Corner + Size.Grid * vGridPos;

					Assets.SpriteBatch.Draw(Assets.Grid,vFinalPos,Color.White);

					if(Table.AnchorCell[nX,nY] == 'X') {
						var vOffset = 0.5f * (Size.Grid - Size.PlayerOne);
						Assets.SpriteBatch.Draw(Assets.PlayerOne,vFinalPos + vOffset,Color.White);
					} else if(Table.AnchorCell[nX,nY] == 'O') {
						var vOffset = 0.5f * (Size.Grid - Size.PlayerTwo);
						Assets.SpriteBatch.Draw(Assets.PlayerTwo,vFinalPos + vOffset,Color.White);
					}
				}
			}
			//_gameState = _table.WhoWins();
			DrawGameState();
			Assets.SpriteBatch.Draw(Assets.Cursor,Input.MousePosition,Color.White);
			Assets.SpriteBatch.End();
		}

		private static void DrawGameState() {
			switch(Table.CurrentGameState) {
				case Table.GameState.PlayerXWins:
					RenderWinnerX();
					break;
				case Table.GameState.PlayerOWins:
					RenderWinnerO();
					break;
				case Table.GameState.IsDraw:
					RenderDraw();
					break;
				default:
					Table.CurrentGameState = Table.GameState.IsPlaying;
					break;
			}
		}

		private static void RenderDraw() { }

		private static void RenderWinnerO() {
			Assets.SpriteBatch.DrawString(Assets.Font,"A.I WIN",new Vector2(400.0f,20f),Color.Red,0.0f,Vector2.Zero,new Vector2(1f,1f),SpriteEffects.None,0.0f);
		}

		private static void RenderWinnerX() {
			Assets.SpriteBatch.DrawString(Assets.Font,"YOU WIN!",new Vector2(400.0f,20f),Color.Red,0.0f,Vector2.Zero,new Vector2(1f,1f),SpriteEffects.None,0.0f);
		}


	}
}
